<template>
  <div class="lookForMonth_wrapper">
    <div class="lookForMonth_top">
      <div class="selectDate">
        <div class="upDownSelect">
          <div class="upDownSelect_item" @click="dateUp"></div>
          <div>{{year}} - {{month | showNum}}</div>
          <div class="upDownSelect_item" @click="dateDown"></div>
        </div>
      </div>
    </div>
    <div class="calendar" :style="calendarStyle">
      <div v-for="item in weeks" class="calendar_item week_item" :key="item.id">
        <p class="dateEdit">{{item.label}}</p>
      </div>
      <div
        v-for="(item,index) in calendarData"
        class="calendar_item"
        :key="index"
        :class="{ash:item.color==='ash'}"
      >
        <p class="dateEdit" :class="{ active:item.label == date && nowMonth == month && year == nowyear }">
          {{item.label | showNum}}
        </p>
      </div>
      <div class="bgBox">
        <span>{{month}}</span>
      </div>
    </div>
    <button @click="backToday">回到今天</button>
  </div>
</template>
<script>
export default {
  name: "Calendar",
  data() {
    return {
      weeks: [
        { label: "日", id: "one" },
        { label: "一", id: "two" },
        { label: "二", id: "three" },
        { label: "三", id: "four" },
        { label: "四", id: "five" },
        { label: "五", id: "six" },
        { label: "六", id: "seven" },
      ],
      calendarData: [], // 日历循环渲染数据
      year: 0, // 当前日期年
      month: 0, // 当前日期月数
      date: 0, // 当前日期号数
      day: -1, // 当前星期几
      nowMonth: 0,//nowMonth,nowyear用来确定给当日加一个独特样式（视觉上知道今天是哪天
      nowyear:0
    }
  },
  filters: {
    showNum(value) {//将不满两位的数字往前补0
      return value.toString().length <= 1 ? "0" + value : value;
    }
  },
  computed: {
    // 根据当月日期详情更改日历表格高度
    calendarStyle() {
      return this.calendarData.length > 35 ? "height: 352px;" : "height: 301px;"
    }
  },
  async created() {
    // 获取当前日期数据
    this.getNow();
    this.getCalendarDate(); // 给calendarData添加当月数据
    this.getNowMonthY();
  },
  methods: {
    // 获取当前时间
    getNow() {
      const now = new Date();
      this.year = +now.getFullYear();
      this.month = +now.getMonth() + 1;
      this.date = +now.getDate();
      this.day = +now.getDay();
    },
    // 获取每个月的天数
    monthDay(month) {
      if ([1, 3, 5, 7, 8, 10, 12].includes(month)) {
        return 31;
      } else if ([4, 6, 9, 11].includes(month)) {
        return 30;
      } else if (month === 2) {
        //  判断当年是否为闰年
        if (
          (this.year % 4 === 0 && this.year % 100 !== 0) ||
          this.year % 400 === 0
        ) {
          return 29;
        } else {
          return 28;
        }
      }
    },
    // 给calendarData添加当月数据
    getCalendarDate() {
      // 获取当前月份一号星期几
      const firstDay = new Date(
        this.year + "-" + this.month + "-" + "01"
      ).getDay();
      this.calendarData = [];
      const num = parseInt(firstDay);
      const nowDays = this.monthDay(this.month);
      const lastMonth = this.month - 1 > 0 ? this.month - 1 : 12;
      const lastDays = this.monthDay(lastMonth);
      //   循环添加上一个月数据
      for (let i = 0; i < num; i++) {
        this.calendarData.push({ label: lastDays - num + i + 1, color: "ash" });
      }
      // 循环添加当月数据
      for (let i = 0; i < nowDays; i++) {
        this.calendarData.push({ label: i + 1 });
      }
      // 循环添加下一个月数据
      if (this.calendarData.length % 7 !== 0) {
        const surplusDay = 7 - (this.calendarData.length % 7);
        for (let i = 0; i < surplusDay; i++) {
          this.calendarData.push({ label: i + 1, color: "ash" });
        }
      }
      this.loading = false;
    },
    // 切上月
    dateUp() {
      this.month--;
      if (this.month <= 0) {
        this.year--;
        this.month = 12;
      }
      this.getCalendarDate(); // 给calendarData添加当月数据
    },
    // 切下月
    dateDown() {
      this.month++;
      if (this.month > 12) {
        this.year++;
        this.month = 1;
      }
      this.getCalendarDate(); // 给calendarData添加当月数据
    },
    getNowMonthY() {
      const date = new Date();
      this.nowMonth = date.getMonth() + 1;
      this.nowyear = date.getFullYear()
    },
    //回当前月
    backToday() {
      this.getNow();
      this.getCalendarDate();
      this.getNowMonthY();
    }
  },
};
</script>
<style lang="scss" scoped>
.lookForMonth_wrapper {
  width: 100vw;
}
.lookForMonth_top {
  margin-bottom: 20px;
  overflow: hidden;
  .selectDate {
    height: 30px;
    line-height: 18px;
    display: flex;
    justify-content: center;
    align-items: center;
    .upDownSelect {
      display: flex;
      justify-content: space-around;
      width: 50%;
      .upDownSelect_item {
        width: 0;
        height: 0;
        border: 7px solid transparent;
        cursor: pointer;
      }
      .upDownSelect_item:nth-child(1) {
        border-right: 7px solid #666;
        // margin-right: 5px;
        &:hover {
          border-right: 7px solid skyblue;
        }
      }
      .upDownSelect_item:nth-child(3) {
        border-left: 7px solid #666;
        &:hover {
          border-left: 7px solid skyblue;
        }
      }
    }
  }
}
/* 日历表样式*/
.calendar {
  position: relative;
  width: 100vw;
  border-bottom: 1px solid #ccc;
  display: flex;
  flex-wrap: wrap;
  box-sizing: border-box;
  .calendar_item {
    box-sizing: border-box;
    width: calc(100% / 7);
    height: 50px;
    text-align: center;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    &.date:hover {
      background: #eee;
    }
    .status {
      margin-top: 10px;
      &.textBlue {
        color: blue;
      }
      &.textRed {
        color: brown;
      }
    }
  }
  .ash {
    color: white;
  }
  .dateEdit {
    margin-bottom: 10px;
  }
  .bgBox {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    height: 200px;
    width: 100vw;
    // 水印字体的样式
    font-size: 200px;
    font-weight: bold;
    color: #999;
    opacity: 0.1;
    text-align: center;
    line-height: 1;
  }
  .week_item{
    border-bottom: 3px solid #F5F5F5;
  }
}
.active {
  border-radius: 50%;
  color: white;
  background-color:#F6AA2B;
}
</style>
